﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace AspNetInfo.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:JQueryUIAlert runat=server></{0}:JQueryUIAlert>")]
    public class JQueryUIAlert : WebControl
    {
        public enum JQueryMessageType { Info, Alert };

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(JQueryMessageType.Info)]
        [Localizable(true)]
        public JQueryMessageType MessageType
        {
            get
            {
                JQueryMessageType t = (JQueryMessageType)ViewState["Type"];
                return (((int)t == 0) ? JQueryMessageType.Info : t);
            }

            set
            {
                ViewState["Type"] = value;
            }
        }
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected override void AddAttributesToRender(
            HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ui-widget");
        }

        protected override void CreateChildControls()
        {
            HtmlGenericControl divAlert = new HtmlGenericControl("div");
            divAlert.Style.Add(HtmlTextWriterStyle.Padding, "0pt 0.7em");
            divAlert.Attributes.Add("class", "ui-state-error ui-corner-all");
            HtmlGenericControl pMessage = new HtmlGenericControl("p");
            HtmlGenericControl spanIcon = new HtmlGenericControl("span");
            spanIcon.Style.Add("float", "left");
            spanIcon.Style.Add(HtmlTextWriterStyle.MarginRight, ".3em");
            switch (MessageType)
            {
                case JQueryMessageType.Info:
                    divAlert.Attributes.Add("class", "ui-state-highlight ui-corner-all");
                    spanIcon.Attributes.Add("class", "ui-icon ui-icon-info");
                    break;
                case JQueryMessageType.Alert:
                    divAlert.Attributes.Add("class", "ui-state-error ui-corner-all");
                    spanIcon.Attributes.Add("class", "ui-icon ui-icon-alert");
                    break;
                default:
                    break;
            }
            
            HtmlGenericControl strongTitle = new HtmlGenericControl("strong");
            strongTitle.InnerText = "Alert:";
            Literal litMessage = new Literal();
            litMessage.Text = " " + Text;
            
            pMessage.Controls.Add(spanIcon);
            pMessage.Controls.Add(strongTitle);
            pMessage.Controls.Add(litMessage);
            divAlert.Controls.Add(pMessage);

            this.Controls.Add(divAlert);

            base.CreateChildControls();
        }
    }
}
